package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.MachineSell;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.MachineSellRepository;
import co.yixiang.modules.shop.service.MachineSellService;
import co.yixiang.modules.shop.service.dto.MachineSellDto;
import co.yixiang.modules.shop.service.dto.MachineSellQueryCriteria;
import co.yixiang.modules.shop.service.mapper.MachineSellMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "machineSell")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MachineSellServiceImpl implements MachineSellService {

    private final MachineSellRepository machineSellRepository;

    private final MachineSellMapper machineSellMapper;

    public MachineSellServiceImpl(MachineSellRepository machineSellRepository, MachineSellMapper machineSellMapper) {
        this.machineSellRepository = machineSellRepository;
        this.machineSellMapper = machineSellMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(MachineSellQueryCriteria criteria, Pageable pageable){
        Page<MachineSell> page = machineSellRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(machineSellMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<MachineSellDto> queryAll(MachineSellQueryCriteria criteria){
        return machineSellMapper.toDto(machineSellRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public MachineSellDto findById(Integer id) {
        MachineSell machineSell = machineSellRepository.findById(id).orElseGet(MachineSell::new);
        ValidationUtil.isNull(machineSell.getId(),"MachineSell","id",id);
        return machineSellMapper.toDto(machineSell);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public MachineSellDto create(MachineSell resources) {
        return machineSellMapper.toDto(machineSellRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(MachineSell resources) {
        MachineSell machineSell = machineSellRepository.findById(resources.getId()).orElseGet(MachineSell::new);
        ValidationUtil.isNull( machineSell.getId(),"MachineSell","id",resources.getId());
        machineSell.copy(resources);
        machineSellRepository.save(machineSell);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            machineSellRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<MachineSellDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MachineSellDto machineSell : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", machineSell.getUserId());
            map.put("标题", machineSell.getTitle());
            map.put("品牌", machineSell.getBrandId());
            map.put("类型id", machineSell.getTypeId());
            map.put("详细内容", machineSell.getMessage());
            map.put("年限", machineSell.getYears());
            map.put("阅读次数", machineSell.getCountNumber());
            map.put("创建时间", machineSell.getCreateTime());
            map.put("是否删除", machineSell.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}